package com.leimingkun;

import java.util.LinkedList;
import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;
public class ThreadController {
    public static void main(String[] args) {
        ThreadPoolExecutor pool = new ThreadPoolExecutor(3, 5, 8, TimeUnit.SECONDS, new ArrayBlockingQueue<>(5), new ThreadPoolExecutor.AbortPolicy());
        LinkedList linkedList = new LinkedList();
        for (int i = 0; i < 5000; i++) {
            linkedList.add(i);
        }
        try {
            LinkedBlockingQueue linkedBlockingQueue = new LinkedBlockingQueue(linkedList);
            for (int i = 0; i < 10; i++) {
                pool.execute(new Runnable() {
                    int i=0;
                    @Override
                    public void run() {
                        while (i<50){
                            try {
                                Object poll = linkedBlockingQueue.poll(0, TimeUnit.SECONDS);
                                i++;
                                if(poll==null){
                                    break;
                                }
                                System.out.println(Thread.currentThread().getName()+"--"+poll);
                            } catch (Exception e) {
                                throw new RuntimeException(e);
                            }
                        }
                    }
                });
            }
        }catch (Exception e){
            throw new RuntimeException("超出队列容量");
        }finally {
            pool.shutdown();
        }
    }
}
